<template>
	<div class="content-page">
		<div class="content-nav">
			<el-button size="small" plain type="primary" @click="addShipper" icon="arrow-left">添加快递</el-button>
			<el-button size="small" @click="goBackPage" icon="arrow-left">返回</el-button>
		</div>
		<div class="content-main">
			<div class="filter-box">
				<el-form :inline="true" :model="filterForm" class="demo-form-inline" @submit.prevent>
					<el-form-item label="快递公司">
						<el-input v-model="filterForm.name" placeholder="搜索快递公司" @change="onSubmitFilter"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="onSubmitFilter" size="small">查询</el-button>
					</el-form-item>
				</el-form>
			</div>
			<div class="form-table-box">
				<el-table :data="tableData" style="width: 100%" border stripe>
					<el-table-column prop="id" label="ID" width="100">
					</el-table-column>
					<el-table-column prop="name" label="名字" width="220">
					</el-table-column>
					<el-table-column prop="code" label="代号" width="220">
					</el-table-column>
					<el-table-column prop="sort_order" label="排序" width="220">
					    <template #default="scope">
					        <el-input-number class="sort-width" size="mini" :min="1" :max="100" controls-position="right" v-model="scope.row.sort_order" placeholder="排序" @blur="submitSort(scope.$index, scope.row)" @change="submitSort(scope.$index, scope.row)"></el-input-number>
					    </template>
					</el-table-column>
					<el-table-column label="使用" width="80">
					    <template #default="scope">
					        <el-switch
                      v-model="scope.row.enabled"
                      active-text=""
                      inactive-text=""
                      @change='changeStatus($event,scope.row)'>
					        </el-switch>
					    </template>
					</el-table-column>
					<el-table-column label="操作">
						<template #default="scope">
							<el-button size="small" @click="handleRowEdit(scope.$index, scope.row)">编辑</el-button>
							<el-button size="small" type="danger" @click="handleRowDelete(scope.$index, scope.row)">删除</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<div class="page-box">
				<el-pagination @current-change="handlePageChange" :current-page="page" :page-size="10" layout="total, prev, pager, next, jumper" :total="total">
          <template slot="total">总数</template>
				</el-pagination>
			</div>
		</div>
	</div>
</template>

<script>
import { addShipper , getShipperById, updateShipper ,getShipperList, deleteShipper } from '@/api/shipper'
export default {
	data() {
		return {
			page: 1,
      pageSize:10,
			total: 0,
			filterForm: {
				name: ''
			},
			tableData: []
		}
	},
	methods: {
		async submitSort(index, row){
      // name和code是更新的时候必填的
      let data = {
        id:row.id,
        name: row.name,
        code: row.code,
        sort_order: row.sort_order
      }
      let res = await updateShipper(data)
      if(res.code !== 0){
        this.$message({
          type:'error',
          message:'更新失败'
        })
      }
		},
    goBackPage() {
        this.$router.go(-1);
    },
		handlePageChange(val) {
			this.page = val;
			//保存到localStorage
			// localStorage.setItem('shipperPage', this.page)
			// localStorage.setItem('shipperFilterForm', JSON.stringify(this.filterForm));
			this.getList()
		},
		addShipper() {
			this.$router.push({ name: 'addShipper'})
		},
		handleRowEdit(index, row) {
			this.$router.push({ name: 'addShipper', query: { id: row.id } })
		},
		 handleRowDelete(index, row) {
			this.$confirm('确定要删除?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(async() => {
        let res = await deleteShipper({id:row.id})
        if(res.code === 0){
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList()
        }else{
          this.$message({
            type: 'error',
            message: res.msg
          });
        }
			});
		},
		onSubmitFilter() {
			this.page = 1
			this.getList()
		},
		async getList() {
      let res = await getShipperList({
        page:this.page,
        pageSize:this.pageSize,
        name:this.filterForm.name
      })
      if(res.code == 0){
          this.tableData = res.data.data
          this.page = res.data.page
          this.total = res.data.total
          for(const item of this.tableData){
            item.enabled = item.enabled ? true : false
          }
      }
		},
		async changeStatus($event, row) {
      console.log('changeStatus $event:',$event)
      let data = {
        id: row.id,
        name: row.name,
        code: row.code,
        enabled: $event ? 1 : 0
      }
      let res = await updateShipper(data)
      if(res.code !== 0){
        this.$message({
          type:'error',
          message:'更新失败'
        })
      }
		},
	},
	components: {

	},
	mounted() {
		this.getList();
	}
}

</script>

<style  lang="scss" scoped>
.content-page{
  .content-nav{
    text-align: right;
  }
  .content-main{
    margin-top:20px;
  }
  .page-box{
    margin-top: 30px;
    text-align: right;
  }
}


</style>
